package com.flavors.wan.main.ui.Activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.flavors.wan.main.bean.UserBean;
import com.flavors.wan.main.ui.BaseActivity;
import com.flavors.wan.main.vm.WanVM;
import com.plat.sdk.R;
import com.plat.sdk.okgo.callbck.XmResource;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * EditText 获取焦点自动弹出软键盘 失去焦点自动退出软键盘
 */
public class LoginActivity extends BaseActivity {
    @BindView(R.id.login)
    Button login;
    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.password)
    EditText password;
    private String mPassword;
    private String mUsername;
    private WanVM wanVM;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void loadData() {
        wanVM = ViewModelProviders.of(this).get(WanVM.class);
        wanVM.getLoginData().observe(this, new Observer<XmResource<UserBean>>() {
            @Override
            public void onChanged(XmResource<UserBean> loginBeanXmResource) {
                loginBeanXmResource.handle(new SimpleOnHandleCallBack<UserBean>() {
                    @Override
                    public void onSuccess(UserBean data) {
                        super.onSuccess(data);
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        finish();
                    }

                    @Override
                    public void onError(int code, String message) {
                        super.onError(code, message);
                        Toast.makeText(LoginActivity.this, "登录失败" + code + "" + message, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    @Nullable
    @OnClick(R.id.login)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login:
                //登录成功自动跳转到MainActivity
                mUsername = getEditTextString(username);
                mPassword = getEditTextString(password);
                wanVM.fetchLogin("ydong", "123456");
                break;
        }
    }


    public String getEditTextString(EditText editText) {
        return editText.getText().toString().trim();
    }


}
